package com.kitty.game.mount;


import com.kitty.game.mount.message.ReqMountConvert;
import com.kitty.game.mount.message.ReqMountTimeAdd;
import com.kitty.game.mount.message.ReqSubmitMultiItem;
import com.kitty.game.mount.message.ReqZhaohuan;
import com.kitty.game.pet.model.Pet;
import com.kitty.game.role.model.Role;
import com.kitty.common.utils.SpringUtils;
import com.kitty.game.enter.FiedValue;
import com.kitty.game.enter.RespUpdate;
import com.kitty.game.pet.service.PetService;
import com.kitty.game.team.message.RespMsg;
import com.kitty.common.model.Reason;
import com.kitty.mina.annotation.RequestMapping;
import com.kitty.mina.cache.SessionUtils;
import com.kitty.mina.message.MessagePusher;
import com.kitty.game.mount.service.MountService;
import com.kitty.game.role.service.RoleService;
import org.apache.mina.core.session.IoSession;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;

import java.util.ArrayList;
import java.util.Date;

@Controller
public class MountController {
    Logger logger = LoggerFactory.getLogger(MountController.class);

    @RequestMapping
    public void summon(IoSession session, ReqZhaohuan reqZhaohuan) {
        SpringUtils.getBean(MountService.class).zhaohuan(session, reqZhaohuan);
    }

    @RequestMapping
    public void submitItem(IoSession session, ReqSubmitMultiItem submitMultiItem) {
        SpringUtils.getBean(MountService.class).submitMultiItem(session, submitMultiItem);
    }


    @RequestMapping
    public void convert(IoSession session,ReqMountConvert reqMountConvert){
        Role role =SessionUtils.getRoleBySession(session);
        SpringUtils.getBean(MountService.class).convert(role,reqMountConvert.getPos());
    }
    /**
     * 元宝增加风灵丸时间
     *
     * @param session
     * @param reqMountTimeAdd
     */
    @RequestMapping
    public void MountTimeAdd(IoSession session, ReqMountTimeAdd reqMountTimeAdd) {
        Role role = SessionUtils.getRoleBySession(session);
        if (role == null) {
            return;
        }
        PetService petService = SpringUtils.getBean(PetService.class);
        RoleService roleService = SpringUtils.getBean(RoleService.class);
        Pet pet = petService.getPetByPos(reqMountTimeAdd.getPos(), role);
        if (pet == null) {
            return;
        }
        if (pet.getItype() <= 0) {
            return;
        }
        int currTime = new Long(new Date().getTime() / 1000).intValue();
        int mountTime = pet.getMountTime();
        if (mountTime == 0) {
            mountTime = currTime;
        }
        int needYuanbao = 1766;
        boolean limit = false;
        int DBGold = role.getGold();
        int DBSiver = role.getSiver();
        if (reqMountTimeAdd.getType().equals("gold_coin")) {//金元宝增加
            if (DBGold < needYuanbao) {
                MessagePusher.pushMessage(session, new RespMsg("你的元宝不足。"));
                return;
            }
            SpringUtils.getRoleService().subtractGold(role, needYuanbao, Reason.MOUNT_TIME_ADD);
        } else {//银元宝增加
            if (DBSiver < needYuanbao) {
                if (DBGold < needYuanbao) {
                    MessagePusher.pushMessage(session, new RespMsg("你的元宝不足。"));
                    return;
                }
                SpringUtils.getRoleService().subtractGold(role, needYuanbao, Reason.MOUNT_TIME_ADD);
            } else {
                limit = true;
                roleService.subtractSiver(role, needYuanbao, Reason.MOUNT_TIME_ADD);
            }
        }
        if (limit) {
            int limitTime = pet.getLimitTime();
            if (limitTime == 0) {
                limitTime = -currTime;
            }
            pet.setLimitTime(limitTime - 86400 * 30);
        }
        roleService.updateRoleGoldAndSiver(role);
        pet.setMountTime(mountTime + 86400 * 30);

        role.save();

        int moveSpeed = 9 * 5;
//        if (moveSpeed > pet.getRank() * 5) {
//            moveSpeed = pet.getRank() * 5;
//        }
        RespUpdate respUpdate = new RespUpdate();
        respUpdate.setRoleId(pet.getId());
        respUpdate.setList(new ArrayList<>());
        {
            FiedValue mountField = new FiedValue();
            mountField.setType((short) 270);
            mountField.setVT((byte) 3);
            mountField.setValue(pet.getLimitTime());
            respUpdate.getList().add(mountField);
        }
        {
            FiedValue mountField = new FiedValue();
            mountField.setType((short) 883);
            mountField.setVT((byte) 3);
            mountField.setValue(pet.getMountTime());
            respUpdate.getList().add(mountField);
        }
        {
            FiedValue mountField = new FiedValue();
            mountField.setType((short) 884);//坐骑移动速度
            mountField.setVT((byte) 3);
            mountField.setValue(moveSpeed);
            respUpdate.getList().add(mountField);
        }
        MessagePusher.pushMessage(session, respUpdate);
    }
}
